<?php
function player(){
	global $_SGET,$_SPOST,$_SREQUEST;
	global $auid,$valid;
	$id = $_SGET['id'];
	$firstname = $_SGET['firstname'];
	$lastname = $_SGET['lastname'];
	$position = $_SGET['position'];
	$team = $_SGET['team'];
	$sortby = $_SGET['sortby'];
	$order = $_SGET['order'];
	$startat = $_SGET['startat'];
	$shownr = $_SGET['shownr'];


	$where="WHERE 1 ";
	if ($id){
		$where.=" AND id='".$id."'";
	}
	if ($firstname){
		$where.=" AND firstname='".$firstname."'";
	}
	if ($lastname){
		$where.=" AND lastname='".$lastname."'";
	}
	if (isset($_SGET['team']) AND $team!="All teams" AND $team){
		$where.=" AND team='".$team ."'";
	}
	if (isset($_SGET['position']) AND $position!="All positions" AND $position){
		$where.=" AND position='".$position."'";
	}

	if (isset($_SGET['order'])){
		$order2=$order;
	}else{
		$order2="ASC";
	}
	if ($sortby!=""){
		$sortby2=$sortby;
	}else{
		$sortby2="lastname";
	}
	if (!$startat){
		$startat=0;
	}
	if (!$shownr){
		$shownr=30;
	}
	$query="SELECT * FROM players ".$where." ORDER BY ".$sortby2." ".$order2." LIMIT ".$startat." , ".$shownr;
	$result = mysql_query($query);
	$rows = array();
	while($r = mysql_fetch_assoc($result)){
		$r['firstname']=utf8_encode($r['firstname']);
		$r['lastname']=utf8_encode($r['lastname']);
		$r['team']=utf8_encode($r['team']);
		$rows[] = $r;
	}
	print json_encode($rows);
}
?>